package com.lovo.csc.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.commom.WebRemote.IWebUserRemote;
import com.lovo.commom.remoteMethod.ISysUserRemote;
import com.lovo.commom.util.ConstantUtil;
import com.lovo.csc.bean.DepositHistoryBean;
import com.lovo.csc.bean.DiscountRateBean;
import com.lovo.csc.bean.UserBean;
import com.lovo.csc.bean.UserInfoBean;
import com.lovo.csc.mapper.IDepositHistoryMapper;
import com.lovo.csc.mapper.IDiscountRateMapper;
import com.lovo.csc.mapper.IUserInfoMapper;
import com.lovo.csc.mapper.IUserMapper;
import com.lovo.csc.service.IUserService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@Transactional
public class UserServiceImpl implements IUserService {


    @Autowired
    IUserMapper userMapper;
    @Autowired
    IUserInfoMapper userInfoMapper;
    @Autowired
    IDepositHistoryMapper depositHistoryMapper;
    @Autowired
    IWebUserRemote webUserRemote;

    @Autowired
    ISysUserRemote sysUserRemote;

    @Autowired
    private IDiscountRateMapper discountRateMapper;
    @Autowired
    private ISysUserRemote userRemote;


    @Override
    public void add(UserBean userBean) {
        userMapper.insert(userBean);
    }

    @Override
    public Page<UserBean> findByStatus(Integer status, Integer pageNO) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (status == 0) {
            qw.eq("u_auditState", status);
        } else {
            qw.ne("u_auditState", 0);
        }
        return userMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public UserBean findById(long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public UserBean findUserById(long userId) {
        UserBean userBean = userMapper.selectById(userId);
        QueryWrapper<UserInfoBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", userId).orderByDesc("pk_uiId").last("limit 1");
        UserInfoBean userInfoBean = userInfoMapper.selectOne(qw);
        userBean.setUserInfo(userInfoBean);
        if (userBean.getAuditState() == 1) {
            userBean.setAudit("审核通过");
        } else if (userBean.getAuditState() == 2) {
            userBean.setAudit("审核未通过");
        }
        return userBean;
    }

    @Override
    public void updateUserinfo(long userId, Integer status, String str, String adminRealName) {
        UserBean userBean = userMapper.selectById(userId);
        userBean.setAuditState(status);
        QueryWrapper<UserInfoBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", userId).orderByDesc("pk_uiId").last("limit 1");
        UserInfoBean userInfoBean = userInfoMapper.selectOne(qw);
        if (status == 1) {
            userBean.setIsPass(0);
            //远程调用后端接口添加用户信息
            userRemote.add(JSON.toJSONString(userBean));
        }
        if (status == 2) {
            userBean.setIsPass(1);
            userBean.setFirm("");
            userBean.setIdImage("");
            userBean.setAptitudeImg("");
        }
        userMapper.updateById(userBean);
        userInfoBean.setAdminName(adminRealName);
        userInfoBean.setContent(str);
        userInfoBean.setUpdateDate(LocalDate.now());
        userInfoMapper.updateById(userInfoBean);
        //远程调用前端接口修改用户信息 以及用户审核信息表
        webUserRemote.updateUserAuditState(userBean.getUserName(), userBean.getIsPass(), userBean.getAuditState(), str, adminRealName, String.valueOf(LocalDate.now()));
    }


    @Override
    public Page<UserBean> findByUserFreezeStatus(Integer pageNO) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_frozenState", 2);
        return userMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public void updateUserFreezeStatus(long userId, Integer status) {
        UserBean userBean = userMapper.selectById(userId);
        userBean.setFrozenState(status);
        userMapper.updateById(userBean);
        if (status == 1) {
            //远程调用
            webUserRemote.updateUserFreezeStatus(userBean.getUserName(), status, userBean.getFrozenCause());
        }
        userRemote.updateFrozenState(userBean.getUserName(), status);
    }

    @Override
    public void updateMoneyByUserName(String userName, float money) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userName);
        UserBean userBean = userMapper.selectOne(qw);
        userBean.setMoney(userBean.getMoney() + money);
        userMapper.updateById(userBean);
    }

    @Override
    public float getDiscount(String userName) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userName);
        UserBean userBean = userMapper.selectOne(qw);
        QueryWrapper<DepositHistoryBean> qwui = new QueryWrapper<>();
        qwui.select("sum(dh_money) as money").eq("fk_userId", userBean.getUserId());
        DepositHistoryBean depositHistoryBean = depositHistoryMapper.selectOne(qwui);
        QueryWrapper<DiscountRateBean> qwdr = new QueryWrapper<>();
        if (depositHistoryBean == null) {
            return 0.99f;
        }
        qwdr.le("dr_money", depositHistoryBean.getMoney()).orderByDesc("dr_money").last("limit 1");
        DiscountRateBean discountRateBean = discountRateMapper.selectOne(qwdr);
        return discountRateBean.getDiscount();
    }

    @RabbitListener(queues = "queueOne")
    public void getUserInfo(String json) {
        UserBean userBean = JSONObject.parseObject(json, UserBean.class);
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userBean.getUserName());
        UserBean user = userMapper.selectOne(qw);
        if (user == null) {
            userMapper.insert(userBean);
            userBean.getUserInfo().setUserId(userBean.getUserId());
        } else {
            user.setAptitudeImg(userBean.getAptitudeImg());
            user.setIdImage(userBean.getIdImage());
            user.setFirm(userBean.getFirm());
            user.setAuditState(0);
            userMapper.updateById(user);
            userBean.getUserInfo().setUserId(user.getUserId());
        }
        userInfoMapper.insert(userBean.getUserInfo());
        ConstantUtil.WS_MSG.add("用户认证待审核");

    }

    @RabbitListener(queues = "queueDepositHistoryTwo")
    public void getDepositHistory(String json) {
        DepositHistoryBean depositHistoryBean = JSONObject.parseObject(json, DepositHistoryBean.class);
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", depositHistoryBean.getUserName());
        UserBean userBean = userMapper.selectOne(qw);
        depositHistoryBean.setUserId(userBean.getUserId());
        depositHistoryBean.setDepositHistoryId(0);
        depositHistoryMapper.insert(depositHistoryBean);
    }

    @RabbitListener(queues = "frozenQueueTwo")
    public void getFrozenUser(String json) {
        String[] result = json.split("%");
        String userJson = result[0];
        String cause = result[1];
        UserBean userBean = JSONObject.parseObject(userJson, UserBean.class);
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userBean.getUserName());
        UserBean userBean1 = userMapper.selectOne(qw);
        userBean1.setFrozenState(2);
        userBean1.setFrozenCause(cause);
        userMapper.updateById(userBean1);
        ConstantUtil.WS_MSG.add("客户冻结");
    }
}
